#include <esp_now.h>
#include <WiFi.h>
#include <esp_wifi.h>
//////////////////////
//bilibili 小努班 ID：437280309
//////////////////////
esp_now_peer_info_t slave;  //从机结构体
uint8_t data = 0;           //数据
uint32_t count = 0;

typedef struct {
  uint8_t type;
  float distance;
  float time;
  uint8_t data[8];
} data_fmt_t;

uint8_t ScanForSlave(void) {
  uint8_t found = 0;

  int16_t scanResults = WiFi.scanNetworks(false, false, false, 300, 1);  //该函数返回的是WIFI数目，其中(300)指的是最大容纳数量

  Serial.print("发现 ");
  Serial.print(scanResults);
  Serial.println(" 个设备 ");
  for (int i = 0; i < scanResults; ++i) {  //因为有scanResults个设备，所有所有设备都遍历一次

    if (WiFi.SSID(i).indexOf("Slave_Car") == 0) {  //如果第[i]个WIFI的SSID(WIFI名字)是以Slave开头
      //那么就把第[i]个WIFI的BSSSID(MAC地址)数据推入slave.peer_addr[]中,(这步在填写MAC地址)
      sscanf(WiFi.BSSIDstr(i).c_str(), "%x:%x:%x:%x:%x:%x",                  //sscanf 和 sprintf刚好相反感兴趣的可以去了解一下，这个c.str()是用来转字符串的函数
             &slave.peer_addr[0], &slave.peer_addr[1], &slave.peer_addr[2],  //sscanf将字符串解析，分析，得到16进制后送到从机结构体数组中
             &slave.peer_addr[3], &slave.peer_addr[4], &slave.peer_addr[5]);
      Serial.println("Slave_Car");

      found = 1;

      break;
    }
  }
  WiFi.scanDelete();  //删除保存的WIFI释放空间

  if (found) {
    esp_now_add_peer(&slave);  //配对（把另一个ESP32加入列表）
  }

  return found;
}

void setup() {  //1.初始化 2.填MAC地址 3.发送数据
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);                             //连接WIFI,STA模式
  esp_wifi_set_channel(1, WIFI_SECOND_CHAN_NONE);  //设置1通道，无第二通道
  esp_now_init();                                  //ESP now初始化

  slave.channel = 1;  //选择从机的第一通道
}

void loop() {
  if (count++ % 300 == 0) {
    int32_t max_scan = 100;
    while (!ScanForSlave()) {
      if (max_scan-- == 0)
        break;
      delay(100);
    }
  }

  if (Serial.available() > 0) {  // 检测缓冲区是否有数据
    data_fmt_t sd;

    char receivedChar = Serial.read();  // 读取单个字符

    // 回显到串口监视器
    Serial.print("Received: ");
    Serial.println(receivedChar);

    // 在此处添加对接收字符的处理逻辑
    if (receivedChar == '0') {
      sd.type = 0;
      esp_now_send(slave.peer_addr, (uint8_t *)&sd, sizeof(sd));  //发送数据(从机地址,数据地址,数据大小);
      Serial.printf("sd.type %d send\n", sd.type);
    }

    if (receivedChar == '1') {
      sd.type = 1;
      esp_now_send(slave.peer_addr, (uint8_t *)&sd, sizeof(sd));  //发送数据(从机地址,数据地址,数据大小);
      Serial.printf("sd.type %d send\n", sd.type);
    }

    if (receivedChar == '2') {
      sd.type = 2;
      esp_now_send(slave.peer_addr, (uint8_t *)&sd, sizeof(sd));  //发送数据(从机地址,数据地址,数据大小);
      Serial.printf("sd.type %d send\n", sd.type);
    }

    if (receivedChar == '3') {
      sd.type = 3;
      esp_now_send(slave.peer_addr, (uint8_t *)&sd, sizeof(sd));  //发送数据(从机地址,数据地址,数据大小);
      Serial.printf("sd.type %d send\n", sd.type);
    }

    if (receivedChar == '4') {
      sd.type = 4;
      esp_now_send(slave.peer_addr, (uint8_t *)&sd, sizeof(sd));  //发送数据(从机地址,数据地址,数据大小);
      Serial.printf("sd.type %d send\n", sd.type);
    }

    if (receivedChar == '5') {
      sd.type = 5;
      esp_now_send(slave.peer_addr, (uint8_t *)&sd, sizeof(sd));  //发送数据(从机地址,数据地址,数据大小);
      Serial.printf("sd.type %d send\n", sd.type);
    }

    if (receivedChar == '6') {
      sd.type = 6;
      esp_now_send(slave.peer_addr, (uint8_t *)&sd, sizeof(sd));  //发送数据(从机地址,数据地址,数据大小);
      Serial.printf("sd.type %d send\n", sd.type);
    }

    if (receivedChar == '7') {
      sd.type = 7;
      sd.distance = 2.5;
      esp_now_send(slave.peer_addr, (uint8_t *)&sd, sizeof(sd));  //发送数据(从机地址,数据地址,数据大小);
      Serial.printf("sd.type %d send\n", sd.type);
    }

    if (receivedChar == '8') {
      sd.type = 8;
      sd.distance = 2.5;
      esp_now_send(slave.peer_addr, (uint8_t *)&sd, sizeof(sd));  //发送数据(从机地址,数据地址,数据大小);
      Serial.printf("sd.type %d send\n", sd.type);
    }

    if (receivedChar == '9') {
      sd.type = 9;
      sd.time = 2;
      esp_now_send(slave.peer_addr, (uint8_t *)&sd, sizeof(sd));  //发送数据(从机地址,数据地址,数据大小);
      Serial.printf("sd.type %d send\n", sd.type);
    }

        if (receivedChar == 'a') {
      sd.type = 10;
      sd.time = 3;
      esp_now_send(slave.peer_addr, (uint8_t *)&sd, sizeof(sd));  //发送数据(从机地址,数据地址,数据大小);
      Serial.printf("sd.type %d send\n", sd.type);
    }

    Serial.println(sd.type);
  }

  delay(100);
}